Skip to main content

Use const by Default — Let let Mean Something

Concept:
Use const for everything unless a variable needs to be reassigned. Let let signal that mutation is intentional and expected.


🔍 What const actually means (and what it doesn’t)

In JavaScript, const means the variable always points to the same thing.
It’s like a pointer that can’t be redirected.

const obj = { name: "Bob" };
obj.name = "Alice"; // ✅ You can still mutate the object
obj = { name: "Charlie" }; // ❌ Error: can’t reassign a const

💡 Think of const as a fixed pointer. It always points to the same object in memory.

  • You can mutate the contents of the object.
  • You can’t make the pointer point to a new object.

Even this line:

obj = { name: "Charlie" };

is really saying:

obj = new Object({ name: "Charlie" });

You're trying to point obj to a brand new object, but const won’t let you — because that reference is locked.


🧩 One Object, Two References

const someObj = { name: "Bob" };
let newThing = someObj;

newThing.name = "Alice";

console.log(someObj.name); // 'Alice'

What’s going on?

  • There’s one object in memory: { name: "Bob" }.
  • Both someObj and newThing point to it.
  • someObj was declared with const, so we can’t reassign it.
  • newThing was declared with let, so we can reassign it — but until we do, it still points to the same object.

This has nothing to do with how the object behaves — only how the variables behave.

Summary:

VariableDeclared WithCan Reassign?Initially Points to Same Object?Can Mutate the Object?
someObjconst❌ No✅ Yes✅ Yes
newThinglet✅ Yes✅ Yes✅ Yes

Both variables share a reference. The difference is whether you're allowed to change where they point.


🎯 Why use const at all?

It’s not about enforcing deep immutability — it’s about intent.

  • const says: “This variable always points to the same thing.”
  • let says: “This variable might point to something else later.”

Even if the thing it points to is mutable, locking the binding prevents accidental reassignment — and makes the code easier to follow.


🧪 Practical example

const config = { retries: 3 };
config.retries++; // ✅ allowed

let count = 0;
count++; // ✅ expected

// Reassigning `config` would fail
// config = {}; // ❌ error

🧵 Final thought

Even if JavaScript doesn’t give us explicit pointers, this pattern gives you structure:

  • Treat const as the default.
  • Reach for let when you actually need reassignment.
  • Let reassignment — and mutation — be a choice, not an accident.

Let let mean something.